﻿using CaseManagerLibrary.Auth;
using CaseManagerLibrary.Models;
using PSDClientService.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDStorageTool
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();

            this.txtUserName.Text = "档案备份管理员";
            //this.txtUserName.Text = "录像下载管理员";
            this.txtPassword.Text = "";
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //var result = LoginService.Login(AppSettings.Setting.ApiServerAddress, this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim());
            HResult result;
            if (String.IsNullOrEmpty(AppSettings.Setting.PemFile))
            {
                result = LoginService.Login(AppSettings.Setting.ApiServerAddress, this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim());
            }
            else
            {
                string pemFile = System.IO.Path.Combine(Application.StartupPath, AppSettings.Setting.PemFile);
                result = LoginService.LoginRSA(AppSettings.Setting.ApiServerAddress, pemFile, this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim());
            }

            if (result.success)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(result.message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
